//查询控制，应该就只有这个一个函数。返回的数据要包装一下。是不是要重新建立一个对象。
var flightModel = require("../models/flight.model");
var companyModel = require("../models/company.model");
var planeModel = require("../models/plane.model");
let obj = {}, newarr = [], right = true;

function getPlane(arr, res){
    var i = 0;
    newarr = []
    function callback(err, planedata){
        if(err){
            res.json({
                success:false,
                description:"plane表查询出错"
            })
        }else{
            if(planedata.length){
                planedata = planedata[0];
                obj = {};
                obj.id = arr[i]._id;
                obj.flight = {
                    company: arr[i].company,
                    plane: arr[i].plane,
                    type: arr[i].type,
                    food: arr[i].food,
                    child: arr[i].child,
                    baby: arr[i].baby
                };
                obj.start = {
                    city: arr[i].startCity,
                    airport: arr[i].startAirport,
                    time: arr[i].startTime
                };
                obj.end = {
                    city: arr[i].endCity,
                    airport: arr[i].endAirport,
                    time: arr[i].endTime
                };
                if(arr[i].economyClass && planedata.economyClass){
                    obj.economyClass = {...arr[i].economyClass, ...planedata.economyClass, afterPrice: arr[i].economyClass.discount * arr[i].economyClass.price }
                }
                if(arr[i].businessClass && planedata.businessClass){
                    obj.businessClass = {...arr[i].businessClass, ...planedata.businessClass, afterPrice: Number(arr[i].businessClass.discount) * Number(arr[i].businessClass.price) }
                }
                if(arr[i].firstClass && planedata.firstClass){
                    obj.firstClass = {...arr[i].firstClass, ...planedata.firstClass, afterPrice: Number(arr[i].firstClass.discount) * Number(arr[i].firstClass.price) }
                }
                newarr.push(obj);
                i++;
            }else{
                res.json({
                    success:false,
                    description:"查询飞机出错",
                    data:{}
                })
                return;
            }
        }
        if(i < arr.length){
            planeModel.find({company: { $regex: new RegExp(arr[i].company)}, plane: {$regex: new RegExp(arr[i].plane)}}).exec(callback)
        }else{
            res.json({
                success:true,
                description:"返回结果",
                data: newarr
            })
        }
    }
    planeModel.find({company: { $regex: new RegExp(arr[i].company)}, plane: {$regex: new RegExp(arr[i].plane)}}).exec(callback);
}

module.exports = function getFlight(req, res, next){
    let query = req.query,
        filter = {};
    if(req.query.goCity && query.toCity && query.date){
        filter = {
            "startCity":query.goCity,
            "startTime": {$regex : new RegExp(query.date)},
            "endCity": query.toCity
        }
        if(query.checkChild === "true"){
            filter.child = true;
        }
        if(query.checkBaby === "true"){
            filter.baby = true;
        }
        if(query.classType === "0"){}
        else if(query.classType === "1"){//判断这个字段是否存在
            filter.economyClass = {$exists:true};
        }else if(query.classType === "2"){
            businessClass = {$exists:true};
        }else if(query.classType === "3"){
            firstClass = {$exists:true};
        }else{}
        flightModel.find(filter).sort({"startTime": 1}).exec((err, data)=>{
            if(err){
                console.error(err);
                res.json({
                    success:false,
                    description:"查询出错",
                    data:data
                })
            }else{
                if(data.length){
                    getPlane(data, res);
                }
                else{
                    res.json({
                        success:true,
                        description:"没有满足条件的航班",
                        data:[]
                    })
                }
            }
        })
    }else{
        res.json({
            success:false,
            description:"字段不完整，无法查询，请输入所有字段后重试",
        })
    }
}
